﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;

namespace Milkify.ComponentModel
{
    public class SuspendableObservableCollection<T> : ObservableCollection<T>
    {
        public bool Suspended { get; private set; }

        public void SuspendNotifications()
        {
            Suspended = true;
        }

        public void ResumeNotifications(bool sendResetNotification)
        {
            Suspended = false;
            
            if (sendResetNotification)
            {
                OnPropertyChanged(new PropertyChangedEventArgs("Count"));
                OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
                OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
            }
        }

        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (!Suspended)
                base.OnCollectionChanged(e);
        }

        protected override void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (!Suspended)
                base.OnPropertyChanged(e);
        }
    }
}
